<template>
    <div class="main-contain">
      <div id="admin">
        <div class="pos" v-loading="state.loading">
          <p class="adminh1">用户注册</p>
          <el-form ref="formRef" label-width="80px" :status-icon="true" :model="login" :rules="rules">
            <el-form-item prop="mobile" label="手机号">
              <el-input v-model="login.mobile" placeholder="手机号" prefix-icon="iphone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="nickName" label="昵称">
              <el-input v-model="login.nickName" placeholder="昵称" prefix-icon="user" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="birthday" label="出生年月">
              <el-date-picker style="width:100%;"
                v-model="login.birthday"
                type="date"
                placeholder=""
              />
            </el-form-item>
            <el-form-item prop="gender" label="性别">
              <el-radio-group v-model="login.gender" size="small">
                <el-radio-button label="男" value="男" />
                <el-radio-button label="女" value="女" />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="头像" prop="role">
              <el-upload 
                class="avatar-uploader showUploader"
                ref="businessLicense"
                action
                :auto-upload="false"
                :show-file-list="false"
                :on-remove="handleRemoveLogo"
                :on-change="uploadLogoFile"
              >
              <ul class="img-list">
                <li v-if="login.pic==''" class="addpic"><el-icon><Plus/></el-icon></li>
              </ul>
              </el-upload>
              <ul class="img-list" v-if="login.pic!=''">
                <li class="img-li">
                  <img :src="login.pic" class="avatar aspect-fill" style="width: 250px;height: 180px;" />
                  <i class="logo-remove el-icon-delete" @click="handleRemoveLogo"><el-icon><Delete /></el-icon></i>
                </li>
              </ul>
            </el-form-item>
            <el-form-item prop="pwd" class="label" label="登录密码">
              <el-input placeholder="密码" v-model="login.pwd" prefix-icon="lock" autocomplete="off"  show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Check" @click="onLogin">提交注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { CacheToken } from "@/constants/cacheKey";
import baseService from "@/service/baseService";
import { setCache } from "@/utils/cache";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { fileUploader } from '@/utils/upload.js'

const router = useRouter();

const state = reactive({
  loading: false
});

const login = reactive(
  { nickName: "", pwd: "", gender: "男", mobile: "", 
  birthday: '', status: 1, pic: '', interestString: '', points: 10
});

const formRef = ref();

const rules = ref({
  mobile: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  pwd: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
});

const interests = ref([
    // 你可以根据需要添加更多兴趣项
]);

// 绑定用户选择的兴趣项
const selectedInterests = ref([]);

/**
 * 注册
 */
const onLogin = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      login.interestString=selectedInterests.value.join(",")
      baseService
        .post("/api/user/register", login)
        .then((res) => {
          state.loading = false;
          if (res.code === 200) {
            setCache(CacheToken, res.data);
            ElMessage.success("注册成功");
            router.push("/user/login");
            // router.push({path:'/sys/user',query:{}})
          } else {
            ElMessage.error(res.msg);
          }
        }).catch((err) => {
          console.error(err)
          state.loading = false;
        });
    }
  });
};

function handleInterestChange(newSelection){
  console.log('用户选择了:', newSelection, selectedInterests.value);
}

  // 上传LOGO图片
  function uploadLogoFile(file: any){
    const typeArr = ["image/png", "image/gif", "image/jpeg", "image/jpg"];
    const isJPG = typeArr.indexOf(file.raw.type) !== -1;
    const isLt3M = file.size / 1024 / 1024 < 3;
    if (!isJPG) {
      ElMessage.error("只能是图片!");
      return;
    }
    if (!isLt3M) {
      ElMessage.error("上传图片大小不能超过 3MB!");
      return;
    }
    var formData = new FormData();
    formData.append("myFile", file.raw)
    fileUploader(formData).then((res) => {
      console.log('LOGO上传成功', res)
      if (res.data.errno==0) {
        login.pic=res.data.data[0]
      } else {
        // ElMessage.error(res.data.message);
      }
    })
  }
  // 移除LOGO图片
  function handleRemoveLogo(file: any){
    login.pic=''
    console.log('移除LOGO图片！',login.pic)
  }
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
body {
  background: rgb(135, 206, 235);
}
.main-contain{
  width: 100%;
  height: 80vh;
  background-image: url('../../../assets/images/bg.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  padding-top: 100px;
}
#admin {
  color: #333;
  margin: 0 auto;
  width: 40%;
  height: auto;
  background: rgba(255,255,255,.8);
  border-radius: 10px;
  box-shadow: 8px 10px 10px rgba(0,0,0,.3);
  border: 1px solid #00aeec;
  padding: 0 50px;
}
.adminh1 {
  font-size: 22px;
  margin: 20px 0;
  text-align: center;
  color: #333;
}
.login-form {
  margin: 0 auto;
  display: block;
  width: 420px;
}
#admin .el-form-item__content{
  margin: 0;
  text-align: center;
}
.adminh3{
  font-size: 16px;
  margin: 10px 0 50px;
  text-align: center;
  color: #333;
}
.label{
  color:#000 !important;
}
.addpic{
  font-size: 22px;
  padding: 20px 25px 15px 25px;
  border: 2px dashed #666;
  color: #888;
  height: 68px;
}
.img-list{
  display: flex;
  flex-wrap: wrap;
}
.img-list .img-li{
  width: 260px;
  position: relative;
  margin-right: 10px;
}
.logo-remove{
  font-size: 22px;
  position: absolute;
  top: 10px;
  right: 10px;
  color: #f00;
  cursor: pointer;
  text-shadow: #000 4px 4px 5px;
}
.logo-remove .el-icon{
  box-shadow: #fff 3px 5px 6px;
  background-color: #fff;
}
.logo-remove:HOVER{
  color: #F56C6C;
}
</style> 